package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.application.LoveApplication;
import com.love_weinuo.bean.LoginCallbackBean;
import com.love_weinuo.databinding.ActivityLoginBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.MainActivity;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.PermissionUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.ToastUtil;
import com.love_weinuo.util.rx.RxActivityTool;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.log.QLog;

import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;
import okhttp3.Response;

import static com.love_weinuo.bean.Constants.LOGINPRESSION;
import static com.love_weinuo.bean.Constants.REQEEST_CDOE_GPS;

/**
 * Created by Administrator
 * on 2019/2/22.
 */
public class LoginActivity extends BaseActivity {

    ActivityLoginBinding binding;
    String name = (String) SpUtils.get("tarot", "");
    String pwd = (String) SpUtils.get("loginPwd", "");
    String moduleName, modulePwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        HTTPAPI.getInstance().getRegisterCode("15515291707", new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("获取发送验证码成功-->"+ s);
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("发送验证码错误"+ e.getMessage());
            }
        });

        add(this);
        moduleName = getIntent().getStringExtra("module_user_name");
        modulePwd = getIntent().getStringExtra("module_pwd");

        if (!TextUtils.isEmpty(moduleName) && !TextUtils.isEmpty(modulePwd)) {
            binding.phone.setText(moduleName);
            binding.pwd.setText(modulePwd);
        }

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        binding.textViewRegister.setOnClickListener(this);
        binding.textViewForget.setOnClickListener(this);
        binding.textViewMain.setOnClickListener(this);

        if (!TextUtils.isEmpty(name)) {
            binding.phone.setText(name);
        }
        if (!TextUtils.isEmpty(pwd)) {
            binding.pwd.setText(pwd);
        }
        initPermission();
    }

    private void initPermission() {
        if (PermissionUtil.loginPermission(this, LOGINPRESSION)) {
            if (!PermissionUtil.isOPen(LoginActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, REQEEST_CDOE_GPS);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewRegister:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.textViewForget:
                startActivity(new Intent(LoginActivity.this, ForgetActivity.class).putExtra("fromType", "forget"));
                break;
            case R.id.textViewMain:
                final String phone = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    ToastUtil.showToast("请收入格式正确的手机号码");
                    return;
                }

                final String pwd = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入登录密码");
                    return;
                }

                String registrationId;
                if (TextUtils.isEmpty(LoveApplication.getInstance().getRegistrationId())) {
                    registrationId = JPushInterface.getRegistrationID(this);
                } else {
                    registrationId = LoveApplication.getInstance().getRegistrationId();
                }

                HTTPAPI.getInstance().login(phone, pwd, registrationId, new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.e("登录成功" + s);
                        try {
                            LoginCallbackBean bean = GsonUtil.GsonToBean(s, LoginCallbackBean.class);
                            if (0 == bean.getErrcode()) {
                                SpUtils.put("module_user_sign", bean.getResult().getUsersig());
                                SpUtils.put("module_user_name", bean.getResult().getRes().getMobile());
                                SpUtils.put("module_user_id", bean.getResult().getRes().getId() + "");
                                SpUtils.put("tarot", phone);
                                SpUtils.put("loginPwd", pwd);
                                SpUtils.put("module_user_sex", bean.getResult().getRes().getSex() + "");
                                SpUtils.put("module_user_nickname", bean.getResult().getRes().getNickname());
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                RxActivityTool.skipActivityAndFinish(LoginActivity.this, MainActivity.class);
                            } else {
                                ToastUtil.showToast(bean.getMsg());
                            }
                        } catch (Exception e) {
                            LogUtil.e("登录解析失败");
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        LogUtil.e("登录失败");
                    }
                });
                break;
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        finish();
        return true;
    }
}
